#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'Test.php';
require_once 'app/init.php';
require_once 'AirHttpTest.php';
require_once 'models/TestSource.php';
require_once 'models/TestUser.php';
require_once 'models/TestBatch.php';
require_once 'models/TestProject.php';
require_once 'models/TestOrganization.php';

// set up the table(s)
AIR2_DBManager::init();

// dummy data
$u1 = new TestUser();
$u1->save();
$u2 = new TestUser();
$u2->save();
$b1 = new TestBatch();
$b1->batch_name = 'AAAAAA Testbatch';
$b1->batch_user_id = $u1->user_id;
$b1->save();
$b2 = new TestBatch();
$b2->batch_name = 'BBBBBB Testbatch';
$b2->batch_user_id = $u2->user_id;
$b2->batch_public_flag = true;
$b2->save();
$b3 = new TestBatch();
$b3->batch_name = 'CCCCCC Testbatch';
$b3->batch_user_id = $u2->user_id;
$b3->save();
$b4 = new TestBatch();
$b4->batch_name = 'DDDDDD Testbatch';
$b4->batch_user_id = $u1->user_id;
$b4->save();

// shared project between users
$o1 = new TestOrganization();
$o1->UserOrg[0]->uo_user_id = $u1->user_id;
$o1->UserOrg[0]->uo_ar_id = 2;
$o1->UserOrg[1]->uo_user_id = $u2->user_id;
$o1->UserOrg[1]->uo_ar_id = 2;
$o1->ProjectOrg[0]->porg_prj_id = 1;
$o1->ProjectOrg[0]->porg_contact_user_id = $u1->user_id;
$o1->save();

// init browser
$browser = new AirHttpTest();
$browser->set_user($u1);
$browser->set_content_type(AirHttpTest::$JSON); // set to json
$conn = AIR2_DBManager::get_master_connection();

// get uuids from radix
function get_batch_uuids($radix) {
    $res = array();
    foreach ($radix as $batch) $res[] = $batch['batch_uuid'];
    return $res;
}

// check batch order returned
function check_order($batches, $radix) {
    $idx = 0;
    foreach ($radix as $uuid) {
        if (isset($batches[$idx]) && $uuid == $batches[$idx]->batch_uuid) {
            $idx++;
        }
    }
    return ($idx == count($batches));
}


plan(62);


/**********************
 * list batches, in normal order
 */
ok( $resp = $browser->http_get('/batch/'), 'list' );
is( $browser->resp_code(), 200, "list - response code" );
ok( $json = json_decode($resp,true), "list - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( count($data) >= 3, 'list - count' );
ok( in_array($b1->batch_uuid, $data), 'list - b1 present' );
ok( in_array($b2->batch_uuid, $data), 'list - b2 present' );
ok( !in_array($b3->batch_uuid, $data), 'list - b3 not present' );
ok( in_array($b4->batch_uuid, $data), 'list - b4 present' );
ok( check_order(array($b1, $b2, $b4), $data), 'list - order' );


/**********************
 * list owned batches, in normal order
 */
ok( $resp = $browser->http_get('/batch/', array('self'=>1)), 'list self' );
is( $browser->resp_code(), 200, "list self - response code" );
ok( $json = json_decode($resp,true), "list self - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( count($data) == 2, 'list self - count' );
ok( in_array($b1->batch_uuid, $data), 'list self - b1 present' );
ok( !in_array($b2->batch_uuid, $data), 'list self - b2 not present' );
ok( !in_array($b3->batch_uuid, $data), 'list self - b3 not present' );
ok( in_array($b4->batch_uuid, $data), 'list self - b4 present' );
ok( check_order(array($b1, $b4), $data), 'list self - order' );


/**********************
 * Order by batch_last_use
 */
$upd_dtim = 'update batch set batch_upd_dtim = ? where batch_id = ?';
$conn->exec($upd_dtim, array(air2_date(strtotime('2015-10-12')), $b2->batch_id));
$conn->exec($upd_dtim, array(air2_date(strtotime('2014-10-12')), $b4->batch_id));

ok( $resp = $browser->http_get('/batch/', array()), 'sort default' );
is( $browser->resp_code(), 200, "sort default - response code" );
ok( $json = json_decode($resp,true), "sort default - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b2, $b4, $b1), $data), 'sort default - order' );

ok( $resp = $browser->http_get('/batch/', array('sort'=>'batch_last_use', 'dir'=>'DESC')), 'sort last use' );
is( $browser->resp_code(), 200, "sort last use - response code" );
ok( $json = json_decode($resp,true), "sort last use - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b2, $b4, $b1), $data), 'sort last use - order' );

ok( $resp = $browser->http_get('/batch/', array('sort'=>'batch_last_use', 'dir'=>'ASC')), 'sort last ASC' );
is( $browser->resp_code(), 200, "sort last ASC - response code" );
ok( $json = json_decode($resp,true), "sort last ASC - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b1, $b4, $b2), $data), 'sort last ASC - order' );


/**********************
 * Order by batch_name
 */
ok( $resp = $browser->http_get('/batch/', array('sort' => 'batch_name', 'dir' => 'ASC')), 'sort name' );
is( $browser->resp_code(), 200, "sort name - response code" );
ok( $json = json_decode($resp,true), "sort name - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b1, $b2, $b4), $data), 'sort name - order' );

ok( $resp = $browser->http_get('/batch/', array('sort' => 'batch_name', 'dir' => 'DESC')), 'sort name DESC' );
is( $browser->resp_code(), 200, "sort name DESC - response code" );
ok( $json = json_decode($resp,true), "sort name DESC - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b4, $b2, $b1), $data), 'sort name DESC - order' );


/**********************
 * Order by batch_count
 */
$s = new TestSource();
$s->save();
$b2->BatchItem[0]->bitem_type = 'S';
$b2->BatchItem[0]->bitem_xid = $s->src_id;
$b2->save();

ok( $resp = $browser->http_get('/batch/', array('sort' => 'batch_count', 'dir' => 'DESC')), 'sort count' );
is( $browser->resp_code(), 200, "sort count - response code" );
ok( $json = json_decode($resp,true), "sort count - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b2, $b4), $data), 'sort count - order' );

ok( $resp = $browser->http_get('/batch/', array('sort' => 'batch_count', 'dir' => 'ASC')), 'sort count ASC' );
is( $browser->resp_code(), 200, "sort count ASC- response code" );
ok( $json = json_decode($resp,true), "sort count ASC - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b4, $b2), $data), 'sort count ASC - order' );


/**********************
 * Order by user_first_name
 */
$u1->user_first_name = 'AAAA';
$u1->save();
$u2->user_first_name = 'ZZZZ';
$u2->save();

ok( $resp = $browser->http_get('/batch/', array('sort' => 'User.user_first_name', 'dir' => 'ASC')), 'sort user' );
is( $browser->resp_code(), 200, "sort user - response code" );
ok( $json = json_decode($resp,true), "sort user - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b4, $b2), $data), 'sort user - order' );

ok( $resp = $browser->http_get('/batch/', array('sort' => 'User.user_first_name', 'dir' => 'DESC')), 'sort user DESC' );
is( $browser->resp_code(), 200, "sort user DESC - response code" );
ok( $json = json_decode($resp,true), "sort user DESC - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( check_order(array($b2, $b4), $data), 'sort user DESC - order' );


/**********************
 * search by batch_name
 */
ok( $resp = $browser->http_get('/batch/', array('q' => $b1->batch_name)), 'search name' );
is( $browser->resp_code(), 200, "search name - response code" );
ok( $json = json_decode($resp,true), "search name - JSON decode" );
$data = get_batch_uuids($json['radix']);
ok( count($data) >= 1, 'search name - count' );
ok( in_array($b1->batch_uuid, $data), 'search name - b1 present' );
ok( !in_array($b2->batch_uuid, $data), 'search name - b2 not present' );
ok( !in_array($b3->batch_uuid, $data), 'search name - b3 not present' );
ok( !in_array($b4->batch_uuid, $data), 'search name - b4 not present' );



// cleanup ... to avoid cascade failures
$s->delete();
$b1->delete();
$b2->delete();
$b3->delete();
$b4->delete();
